﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace WOWMultiPlay
{
    public partial class Form_ButtonOptions : Form
    {
        public Form_ButtonOptions()
        {
            InitializeComponent();
        }

        private void Form_ButtonOptions_Load(object sender, EventArgs e)
        {
            ShowButtonList();
        }

        private bool IsShowingButtonList = false;

        private void ShowButtonList()
        {
            IsShowingButtonList = true;

            listbox_Buttons.DataSource = null;

            if (Settings.Profile.mpButtons.Count > 0)
            {
                listbox_Buttons.DataSource = Settings.Profile.mpButtons;
                listbox_Buttons.DisplayMember = "ButtonName";
                listbox_Buttons.ValueMember = "It";
            }

            IsShowingButtonList = false;
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            Settings.Profile.mpButtons.Add(new mpButton("New Button", new mpKeyArray("{VK_1,DU}"), 1, new mpTime("500ms")));
            ShowButtonList();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckHotkeyValue())
            {
                MessageBox.Show("Invalid Hotkey!");
                return;
            }
            else
            {
                if (cbHotkey.Checked)
                {
                    int mod = 0;

                    if (cbCtrl.Checked)
                    {
                        mod = WindowsAPI.MOD.MOD_CONTROL;
                    }
                    else if (cbAlt.Checked)
                    {
                        mod = WindowsAPI.MOD.MOD_ALT;
                    }
                    else if (cbShift.Checked)
                    {
                        mod = WindowsAPI.MOD.MOD_SHIFT;
                    }
                    else if (cbWin.Checked)
                    {
                        mod = WindowsAPI.MOD.MOD_WIN;
                    }

                    if (((mpButton)listbox_Buttons.SelectedValue).Hotkey != null)
                    {
                        ((mpButton)listbox_Buttons.SelectedValue).Hotkey.Hotkey = Common.GetVKValue(tbHotkey.Text);
                        ((mpButton)listbox_Buttons.SelectedValue).Hotkey.Mods = mod;
                    }
                    else
                    {
                        ((mpButton)listbox_Buttons.SelectedValue).Hotkey = new mpHotkey(Common.GetVKValue(tbHotkey.Text), mod);
                    }
                }
                else
                {
                    ((mpButton)listbox_Buttons.SelectedValue).Hotkey = null;
                }

                ((mpButton)listbox_Buttons.SelectedValue).KeepWindowMinimize = cbKeepWindowMinimze.Checked;
            }

            Serializer<mpProfile>.Serialize(Settings.Profile, Settings.Profile.ProfilePath);
        }

        private bool CheckHotkeyValue()
        {
            if (cbHotkey.Checked && string.IsNullOrEmpty(tbHotkey.Text))
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (listbox_Buttons.SelectedIndex > -1)
            {
                mpButton mpBtn = (mpButton)listbox_Buttons.SelectedValue;

                if (mpBtn.Hotkey != null)
                {
                    mpBtn.Hotkey.UnregisterGlobalHotKey();
                }

                Settings.Profile.mpButtons.Remove(mpBtn);
                ShowButtonList();
            }
        }

        private void listbox_Buttons_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listbox_Buttons.SelectedIndex > -1)
            {
                mpButton mpBtn = (mpButton)listbox_Buttons.SelectedValue;

                tbButtonName.Text = mpBtn.ButtonName;
                tbKeyArray.Text = mpBtn.mpKeyArray.KeyArray;
                tbRepeat.Text = mpBtn.Repeat.ToString();
                tbRepeatInterval.Text = mpBtn.RepeatInterval.ToString();

                if (mpBtn.Hotkey == null)
                {
                    cbHotkey.Checked = false;
                }
                else
                {
                    cbHotkey.Checked = true;
                    tbHotkey.Text = Common.GetVKName(mpBtn.Hotkey.Hotkey);

                    switch (mpBtn.Hotkey.Mods)
                    {
                        case WindowsAPI.MOD.MOD_CONTROL:
                            cbCtrl.Checked = true;
                            break;
                        case WindowsAPI.MOD.MOD_ALT:
                            cbAlt.Checked = true;
                            break;
                        case WindowsAPI.MOD.MOD_SHIFT:
                            cbShift.Checked = true;
                            break;
                        case WindowsAPI.MOD.MOD_WIN:
                            cbWin.Checked = true;
                            break;
                        default:
                            break;
                    }
                }

                cbKeepWindowMinimze.Checked = mpBtn.KeepWindowMinimize;
            }
            else
            {
                tbButtonName.Text = tbKeyArray.Text = tbRepeat.Text = tbRepeatInterval.Text = string.Empty;
            }
        }

        private void tbButtonName_Leave(object sender, EventArgs e)
        {
            if (!IsShowingButtonList && listbox_Buttons.SelectedIndex > -1)
            {
                if (tbButtonName.Text.Trim().Length > 0)
                {
                    ((mpButton)listbox_Buttons.SelectedValue).ButtonName = tbButtonName.Text;
                }
                else
                {
                    MessageBox.Show("Invalid value.");
                    tbButtonName.Focus();
                }

                ShowButtonList();
            }
        }

        private void tbKeyArray_Leave(object sender, EventArgs e)
        {
            if (!IsShowingButtonList && listbox_Buttons.SelectedIndex > -1)
            {
                try
                {
                    ((mpButton)listbox_Buttons.SelectedValue).mpKeyArray.KeyArray = tbKeyArray.Text;
                }
                catch
                {
                    MessageBox.Show("Invalid value.");
                    tbKeyArray.Focus();
                }

                ShowButtonList();
            }
        }

        private void tbRepeat_Leave(object sender, EventArgs e)
        {
            if (!IsShowingButtonList && listbox_Buttons.SelectedIndex > -1)
            {
                try
                {
                    ((mpButton)listbox_Buttons.SelectedValue).Repeat = int.Parse(tbRepeat.Text);
                }
                catch
                {
                    MessageBox.Show("Invalid value.");
                    tbRepeat.Focus();
                }

                ShowButtonList();
            }
        }

        private void tbRepeatInterval_Leave(object sender, EventArgs e)
        {
            if (!IsShowingButtonList && listbox_Buttons.SelectedIndex > -1)
            {
                try
                {
                    ((mpButton)listbox_Buttons.SelectedValue).RepeatInterval = new mpTime(tbRepeatInterval.Text);
                }
                catch
                {
                    MessageBox.Show("Invalid value.");
                    tbRepeatInterval.Focus();
                }

                ShowButtonList();
            }
        }

        private void llb_KeyViewer_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (!tbKeyArray.ReadOnly)
            {
                tbKeyArray.Focus();
                tbKeyArray.ReadOnly = true;
                tbKeyArray.WatchKeyPress = true;
            }
            else
            {
                tbKeyArray.ReadOnly = false;
                tbKeyArray.WatchKeyPress = false;
            }
        }

        private void llb_ButtonNameHelp_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            MessageBox.Show("Button Name");
        }

        private void llb_KeyArrayHelp_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            MessageBox.Show("Example: {VK_1}{VK_2,DU}{VK_RETURN,D}{VK_A,D,1s}{VK_B,DU,1s,100ms}\r\n"
                            + "{VK_1} means Key 1 will be down and up, equals {VK_1,DU}.\r\n"
                            + "{VK_A,D,1s} means Key A will only be down, after pressing there will be 1s sleep.\r\n"
                            + "{VK_B,DU,1s,100ms} means Key B will be down and up, time between down and up is 100ms, after pressing there will be 1s sleep.");
        }

        private void llb_RepeatHelp_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            MessageBox.Show("Repeat = 2 means repeats 2 times.\r\n"
                            + "Repeat = 0 means this repeats infinitely.");
        }

        private void llb_RepeatIntervalHelp_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            MessageBox.Show("Example:\r\n"
                            + "1. 1h2m3s4ms means 1 hour 2 minutes 3 seconds and 4 ms\r\n"
                            + "2. 5h\r\n"
                            + "3. 3m\r\n"
                            + "4. 10s\r\n"
                            + "5. 20ms\r\n"
                            + "6. all above examples is allowed");
        }

        private void cbHotkey_CheckedChanged(object sender, EventArgs e)
        {
            if (cbHotkey.Checked)
            {
                cbCtrl.Enabled = cbAlt.Enabled = cbShift.Enabled = cbWin.Enabled = tbHotkey.Enabled = true;
            }
            else
            {
                cbCtrl.Enabled = cbAlt.Enabled = cbShift.Enabled = cbWin.Enabled = tbHotkey.Enabled = false;
            }
        }

        bool IscbModsChanging = false;

        private void cbMods_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox cb = (CheckBox)sender;

            if (!IscbModsChanging)
            {
                IscbModsChanging = true;

                cbCtrl.Checked = cbAlt.Checked = cbShift.Checked = cbWin.Checked = false;
                cb.Checked = true;

                IscbModsChanging = false;
            }
        }
    }
}